package com.api.user.service.impl;

import com.api.common.mybatis.ResultMap;
import com.api.core.config.AuthUser;
import com.api.user.dao.UserFriendMapper;
import com.api.user.model.UserFriend;
import com.api.user.model.UserFriendApply;
import com.api.user.service.TaskService;
import com.api.user.service.UserFriendApplyService;
import com.api.user.service.UserFriendService;
import com.api.core.service.AbstractService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;

/**
 * Created by wanghuiwen on 2021/03/31.
 */
@Service
@Transactional
public class UserFriendServiceImpl extends AbstractService<UserFriend> implements UserFriendService {
    @Resource
    private UserFriendMapper userFriendMapper;

    @Resource
    private UserFriendApplyService userFriendApplyService;
    @Resource
    private TaskService taskService;


    @Override
    public Result agree(Long userId, Long fansId,Integer state) {
        UserFriendApply userFriendApply = userFriendApplyService.findByUserIdAndFansId(userId,fansId);
        if(state == 3){
            userFriendApply.setState(state);
            userFriendApplyService.update(userFriendApply);
        }else{
            userFriendApply.setState(state);
            userFriendApplyService.update(userFriendApply);
            UserFriend userFriend = new UserFriend();
            userFriend.setCreateTime(new Date().getTime());
            userFriend.setFansId(fansId);
            userFriend.setIdolId(userId);
            this.save(userFriend);
        }
        taskService.completeTask("6",fansId,1);
        return ResultGenerator.genSuccessResult();

    }
    @Override
    public Result remove(Long userId, Long fansId) {
        UserFriend userFriend = this.getFriend(userId,fansId);
        this.deleteById(userFriend);
        return ResultGenerator.genSuccessResult();

    }

    @Override
    public UserFriend getFriend(Long userId, Long fansId) {
        return userFriendMapper.getFriend(userId,fansId);
    }

    @Override
    public List<UserFriend> getUserFriend(Long id) {
        return userFriendMapper.getUserFriend(id);
    }

    @Override
    public Result applyList(String order, Integer page, Integer size, Long userId) {
        PageHelper.startPage(page, size);
        List<ResultMap<String,Object>> res = userFriendMapper.applyList(userId);
        PageInfo<ResultMap<String,Object>> pageInfo = new PageInfo<>(res);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Override
    public Result search(String uid, AuthUser authUser) {
        List<ResultMap<String,Object>> res =  userFriendMapper.search(uid,authUser.getId());
        return ResultGenerator.genSuccessResult(res);
    }
}
